package com;

import com.com.utils.GameUtils;
import com.obj.BodyObj;
import com.obj.FoodObj;
import com.obj.HeadObj;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {

    //游戏状态 0.未开始 1.游戏中 2.暂停 3.失败 4.通关 5.重新开始
    public static int state = 0;

    //分数
    public int score = 0;

    //速度
    public static int run = 200;

    //定义双缓存图片
    Image staticImg = null;

    //计分窗口的宽高
    int winWidth = 800;
    int winHeight = 600;
    int  scoreWidth=200;

    //创建蛇头对象
    HeadObj headObj = new HeadObj(GameUtils.rightImg, 60, 570, this);

    //蛇身体的集合
    public List<BodyObj> bodyObjList = new ArrayList<>();

    //食物
    public FoodObj foodObj = new FoodObj().getFood();


    public void launch() {
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口位置大小
        this.setSize(winWidth, winHeight);
        //设置窗口的位置
        this.setLocationRelativeTo(null);
        //设置窗口标题
        this.setTitle("代依博的贪吃蛇");

        //身体的初始化
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 30, 570, this));
        bodyObjList.add(new BodyObj(GameUtils.bodyImg, 0, 570, this));

        //键盘时间
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                    switch (state) {
                        //未开始
                        case 0:
                            state = 1;
                            break;
                        //游戏中
                        case 1:
                            state = 2;
                            repaint();
                            break;
                        case 2:
                            //游戏暂停
                            state = 1;
                            break;
                        case 3:
                            //失败重新开始
                            state = 5;
                            break;
                        default:
                            break;
                    }
                }
            }
        });

        while (true) {
            if (state == 1) {
                //游戏中
                repaint();
            }
            if (state == 5) {
                state = 0;
                resetGame();
            }

            try {
                Thread.sleep(run);  //更改刷新速度，即是蛇移动的速度
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //表格绘制
    @Override
    public void paint(Graphics g) {
        //初始化缓存图片
        if (staticImg == null) {
            staticImg = this.createImage(winWidth, winHeight);
        }
        //获取graphics对象
        Graphics gImage = staticImg.getGraphics();
        //白色背景
        gImage.setColor(Color.white);
        gImage.fillRect(0, 0, winWidth, winHeight);
        gImage.setColor(Color.black);
        for (int i = 0; i <= 20; i++) {
            gImage.drawLine(0, i * 30, 600, i * 30); //横线
            gImage.drawLine(i * 30, 0, i * 30, 600); //竖线
        }
        //绘制蛇的身体   反向遍历
        for (int i = bodyObjList.size() - 1; i >= 0; i--) {
            bodyObjList.get(i).paintSelf(gImage);
        }
        //绘制蛇头
        headObj.paintSelf(gImage);

        //绘制食物
        foodObj.paintSelf(gImage);
        //积分板背景绘制
        gImage.setColor(Color.red);
        gImage.fillRect(600, 0, scoreWidth, winHeight);
        //分数绘制
        GameUtils.drawWord(gImage, score + "分", Color.orange, 50, 650, 300);

        //绘制提示语
        gImage.setColor(Color.white );
        prompt(gImage);
        //将缓存的图片绘制到主窗体中
        g.drawImage(staticImg, 0, 0, null);

    }

    //绘制提示语
    void prompt(Graphics g) {
        //未开始
        if (state == 0) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "按下空格游戏开始", Color.black, 35, 170, 290);
        }
        //暂停
        if (state == 2) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "按下空格继续游戏", Color.blue, 35, 150, 290);
        }
        //失败
        if (state == 3) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "游戏失败,按空格重新开始", Color.red, 33, 130, 290);
        }
        //通关
        if (state == 4) {
            g.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(g, "恭喜你通关了", Color.green, 35, 200, 290);
        }
    }

    //游戏重置
    void resetGame() {
        //关闭界面
        this.dispose();
        //开启新窗口
        String[] reset = {};
        main(reset);
    }

    //窗口执行
    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
